home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / cxref.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  8.7 KB  |  280 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/cxref.c 1.13 1996/02/24 14:50:48 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.   ******************/ /******************
  6.   Written by Andrew M. Bishop
  7.  
  8.   This file Copyright 1995,96 Andrew M. Bishop
  9.   It may be distributed under the GNU Public License, version 2, or
  10.   any higher version.  See section COPYING of the GNU Public license
  11.   for conditions under which this file may be redistributed.
  12.   ***************************************/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19.  
  20. #include "parse-yy.h"
  21. #include "memory.h"
  22. #include "datatype.h"
  23. #include "cxref.h"
  24.  
  25. /*+ The default value of the CPP command. +*/
  26. #ifdef CXREF_CPP
  27. #define CPP_COMMAND CXREF_CPP
  28. #else
  29. #define CPP_COMMAND "gcc -E -C -dD"
  30. #endif
  31.  
  32. static void DocumentTheFile(char* name);
  33.  
  34. static char* cpp_command;            /*+ The actual cpp command that is built up, adding -D, -U and -I options. +*/
  35. static unsigned int cpp_command_len; /*+ The length of the cpp command that is built up, adding -D, -U and -I options. +*/
  36.  
  37. /*+ The command line switch that sets the format of the output, +*/
  38. int option_all_comments=0,              /*+ use all comments. +*/
  39.     option_xref=0,                      /*+ do cross referencing. +*/
  40.     option_warn=0,                      /*+ produce warnings. +*/
  41.     option_index=0,                     /*+ produce an index. +*/
  42.     option_raw=0,                       /*+ produce raw output. +*/
  43.     option_latex=0,                     /*+ produce latex output. +*/
  44.     option_html=0;                      /*+ produce html output. +*/
  45.  
  46. /*+ The command line switch for the output name, +*/
  47. char *option_odir=".",                  /*+ The directory to use. +*/
  48.      *option_name="cxref";              /*+ The base part of the name. +*/
  49.  
  50. /*++++++++++++++++++++++++++++++++++++++
  51.   The main function that calls the parser.
  52.  
  53.   int main Returns the status, zero for normal termination, else an error.
  54.  
  55.   int argc The command line number of arguments.
  56.  
  57.   char** argv The actual command line arguments
  58.   ++++++++++++++++++++++++++++++++++++++*/
  59.  
  60. int main(int argc,char** argv)
  61. {
  62.  int i;
  63.  
  64.  cpp_command=(char*)Malloc(cpp_command_len=1+strlen(CPP_COMMAND));
  65.  
  66.  strcpy(cpp_command,CPP_COMMAND);
  67.  
  68.  if(argc==1)
  69.    {
  70.     fputs("Usage: cxref filename [ ... filename]                  ; Filenames to document.\n"
  71.           "             [-Idirname]                               ; The usual gcc switches.\n"
  72.           "             [-Ddefine] [-Udefine]                     ; The usual gcc switches.\n"
  73.           "             [-CPP cpp_program]                        ; The cpp program to use (default '" CPP_COMMAND "')\n"
  74.           "             [-Odirname]                               ; Use dirname as the output directory\n"
  75.           "             [-Nbasename]                              ; Use basename.* as the output filenames\n"
  76.           "             [-all-comments]                           ; Use all comments [Dangerous option!]\n"
  77.           "             [-xref[-all][-file][-func][-var][-type]]  ; Do cross referencing.\n"
  78.           "             [-warn[-all][-comment][-xref]]            ; Produce warnings on comments, cross refs or both.\n"
  79.           "             [-index[-all][-file][-func][-var][-type]] ; Produce an appendix of cross references.\n"
  80.           "             [-raw]                                    ; Produce raw output.\n"
  81.           "             [-latex|-latex2e]                         ; Produce LaTeX output.\n"
  82.           "             [-html]                                   ; Produce HTML output.\n"
  83.           ,stderr);
  84.     exit(1);
  85.    }
  86.  
  87.  for(i=1;i<argc;i++)
  88.    {
  89.     if(!strncmp(argv[i],"-I",2) || !strncmp(argv[i],"-D",2) || !strncmp(argv[i],"-U",2))
  90.       {
  91.        cpp_command=Realloc(cpp_command,cpp_command_len=cpp_command_len+1+strlen(argv[i]));
  92.        strcat(cpp_command," ");
  93.        strcat(cpp_command,argv[i]); argv[i]=NULL;
  94.        continue;
  95.       }
  96.  
  97.     if(!strcmp(argv[i],"-CPP"))
  98.       {
  99.        char *opts=&cpp_command[strlen(CPP_COMMAND)],*old=cpp_command;
  100.        cpp_command=Malloc(cpp_command_len=strlen(opts)+strlen(argv[i+1])+2);
  101.        strcpy(cpp_command,argv[i+1]);
  102.        if(opts[0])
  103.           strcat(cpp_command,opts);
  104.        argv[i++]=NULL;argv[i]=NULL;
  105.        Free(old);
  106.       }
  107.  
  108.     if(!strncmp(argv[i],"-O",2))
  109.       {option_odir=&argv[i][2]; argv[i]=NULL; continue;}
  110.  
  111.     if(!strncmp(argv[i],"-N",2))
  112.       {option_name=&argv[i][2]; argv[i]=NULL; continue;}
  113.  
  114.     if(!strcmp(argv[i],"-all-comments"))
  115.       {option_all_comments=1; argv[i]=NULL;continue;}
  116.  
  117.     if(!strncmp(argv[i],"-xref",5))
  118.       {
  119.        char* p=&argv[i][5];
  120.  
  121.        if(!*p)
  122.           option_xref=XREF_ALL;
  123.        else
  124.           while(*p)
  125.             {
  126.              if(!strncmp(p,"-all" ,4)) {option_xref|=XREF_ALL ; p=&p[4]; continue;}
  127.              if(!strncmp(p,"-file",5)) {option_xref|=XREF_FILE; p=&p[5]; continue;}
  128.              if(!strncmp(p,"-func",5)) {option_xref|=XREF_FUNC; p=&p[5]; continue;}
  129.              if(!strncmp(p,"-var" ,4)) {option_xref|=XREF_VAR ; p=&p[4]; continue;}
  130.              if(!strncmp(p,"-type",5)) {option_xref|=XREF_TYPE; p=&p[5]; continue;}
  131.              break;
  132.             }
  133.        argv[i]=NULL;continue;
  134.       }
  135.  
  136.     if(!strncmp(argv[i],"-warn",5))
  137.       {
  138.        char* p=&argv[i][5];
  139.  
  140.        if(!*p)
  141.           option_warn=WARN_ALL;
  142.        else
  143.           while(*p)
  144.             {
  145.              if(!strncmp(p,"-all"    ,4)) {option_warn|=WARN_ALL    ; p=&p[4]; continue;}
  146.              if(!strncmp(p,"-comment",8)) {option_warn|=WARN_COMMENT; p=&p[8]; continue;}
  147.              if(!strncmp(p,"-xref"   ,5)) {option_warn|=WARN_XREF   ; p=&p[5]; continue;}
  148.              break;
  149.             }
  150.        argv[i]=NULL;continue;
  151.       }
  152.  
  153.     if(!strncmp(argv[i],"-index",6))
  154.       {
  155.        char* p=&argv[i][6];
  156.  
  157.        if(!*p)
  158.           option_index=INDEX_ALL;
  159.        else
  160.           while(*p)
  161.             {
  162.              if(!strncmp(p,"-all" ,4)) {option_index|=INDEX_ALL ; p=&p[4]; continue;}
  163.              if(!strncmp(p,"-file",5)) {option_index|=INDEX_FILE; p=&p[5]; continue;}
  164.              if(!strncmp(p,"-func",5)) {option_index|=INDEX_FUNC; p=&p[5]; continue;}
  165.              if(!strncmp(p,"-var" ,4)) {option_index|=INDEX_VAR ; p=&p[4]; continue;}
  166.              if(!strncmp(p,"-type",5)) {option_index|=INDEX_TYPE; p=&p[5]; continue;}
  167.              break;
  168.             }
  169.        argv[i]=NULL;continue;
  170.       }
  171.  
  172.     if(!strcmp(argv[i],"-raw"))
  173.       {option_raw=1; argv[i]=NULL;continue;}
  174.  
  175.     if(!strcmp(argv[i],"-latex"))
  176.       {if(!option_latex)option_latex=1; argv[i]=NULL;continue;}
  177.     if(!strcmp(argv[i],"-latex2e"))
  178.       {option_latex=2; argv[i]=NULL;continue;}
  179.  
  180.     if(!strcmp(argv[i],"-html"))
  181.       {option_html=1; argv[i]=NULL;continue;}
  182.  
  183.     if(argv[i][0]=='-')
  184.       {fprintf(stderr,"cxref: Unknown option '%s'\n",argv[i]);exit(1);}
  185.  
  186.     if(!strncmp(argv[i],"./",2))
  187.       {argv[i]+=2; continue;}
  188.    }
  189.  
  190.  for(i=1;i<argc;i++)
  191.     if(argv[i])
  192.       {
  193.        File file=NewFile(argv[i]);
  194.  
  195.        DocumentTheFile(argv[i]);
  196.  
  197.        if(option_xref)
  198.           CrossReference(file);
  199.  
  200.        if(option_raw || option_warn)
  201.           WriteWarnRawFile(file);
  202.        if(option_latex)
  203.           WriteLatexFile(file);
  204.        if(option_html)
  205.           WriteHTMLFile(file);
  206.  
  207.        DeleteSpareTypes();
  208.        DeleteSpareProtos();
  209.        TidyMemory();
  210.        DeleteFile(file);
  211.       }
  212.  
  213.  Free(cpp_command);
  214.  
  215.  if(option_index)
  216.    {
  217.     StringList files,funcs,vars,types;
  218.  
  219.     InitStringList(&files);
  220.     InitStringList(&funcs);
  221.     InitStringList(&vars);
  222.     InitStringList(&types);
  223.  
  224.     CreateAppendix(&files,&funcs,&vars,&types);
  225.  
  226.     if(option_raw||option_warn)
  227.        WriteWarnRawAppendix(&files,&funcs,&vars,&types);
  228.     if(option_latex)
  229.        WriteLatexAppendix(&files,&funcs,&vars,&types);
  230.     if(option_html)
  231.        WriteHTMLAppendix(&files,&funcs,&vars,&types);
  232.    }
  233.  
  234.  PrintMemoryStatistics();
  235.  
  236.  return(0);
  237. }
  238.  
  239.  
  240. /*++++++++++++++++++++++++++++++++++++++
  241.   Calls CPP for the file to get all of the needed information.
  242.  
  243.   char* name The name of the file to document.
  244.  
  245.   The CPP is started as a sub-process, (using popen to return a FILE* for lex to use).
  246.   ++++++++++++++++++++++++++++++++++++++*/
  247.  
  248. static void DocumentTheFile(char* name)
  249. {
  250.  static int first_yyin=1;
  251.  char* command;
  252.  struct stat stat_buf;
  253.  
  254.  if(stat(name,&stat_buf)==-1)
  255.    {fprintf(stderr,"cxref: Cannot access the file '%s'\n",name);exit(1);}
  256.  
  257.  command=(char*)Malloc(cpp_command_len+2+strlen(name));
  258.  
  259.  sprintf(command,"%s %s",cpp_command,name);
  260.  
  261.  yyin=popen(command,"r");
  262.  
  263.  if(!yyin)
  264.    {fprintf(stderr,"cxref: Failed to start the cpp command '%s'\n",command);exit(1);}
  265.  
  266.  if(!first_yyin)
  267.     yyrestart(yyin);
  268.  else
  269.     first_yyin=0;
  270.  
  271. #if YYDEBUG
  272.  yydebug=0;
  273. #endif
  274.  yyparse();
  275.  
  276.  pclose(yyin);
  277.  
  278.  Free(command);
  279. }
  280.